<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>初始化 state - 紫霞仙子</title>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component {
            state = {
                isNight: true
            }

            //声明事件回调
            fn = () => {
                //这里的 this 是指向实例对象的
                // console.log(this);
                this.setState({
                    isNight: !this.state.isNight
                })
            }

            // constructor(){
            //     super();
            //     //这个写法等效于上边的  fn = () => {} , this 的指向是实例对象
            //     this.fn = () => {
            //         console.log(this);
            //     }
            // }

            render(){
                //获取 isNight 的值    这里的 this 是指向实例对象的.
                let {isNight} = this.state; //解构赋值
                return <div>
                        <h2>至尊宝, 我是 {isNight ? '青霞' : '紫霞'} 仙子</h2> 
                        <button onClick={this.fn}>切换白天与黑色</button>
                    </div>
            }
        }

        ReactDOM.render(<App />, document.getElementById('root'));
    </script>
</body>
</html>